home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / DynaDoodle / Doodle.m < prev    next >
Text File  |  1993-12-15  |  6KB  |  156 lines

  1. /**------------------------------------*------------------------------------**
  2.     RELEASED TO THE PUBLIC DOMAIN BY CODEWORKS DEVELOPMENT, DECEMBER 1993.
  3.     You may freely copy, distribute and reuse the code in this example. Codeworks disclaims any warranty of any kind, expressed or implied, as to its fitness for any particular use.
  4.     Author:  Andrew Vyrros, av@codeworks.com
  5.  **------------------------------------*------------------------------------**/
  6. /**------------------------------------*------------------------------------**
  7.     File:    Doodle.m
  8.     Project: DynaDoodle
  9.     Info:    Implementation of the Doodle class. Doodle is a subclass of View for creating arbitrary silly drawings. Doodle implements the basic behavior of setting foreground an background colors and loading a nib with custom controls. Subclasses, which are loaded from dynamic modules, provide the specific drawing functionality.
  10.  **------------------------------------*------------------------------------**/
  11.  
  12. #import "Doodle.h"
  13. #import <appkit/appkit.h>
  14.  
  15.  
  16. @implementation Doodle
  17.  
  18.  
  19. /**------------------------------------*------------------------------------**
  20.     Method:  initFrame:
  21.     Info:    Designated initializer, initializes new Doodle instance. Sets foregroundColor and backgroundColor to reasonable defaults.
  22.     Return:  (id)self
  23.  **------------------------------------*------------------------------------**/
  24. - initFrame:(const NXRect *)f
  25. {
  26.   /* Connect to superclass. */
  27.   [super initFrame:f];
  28.   
  29.   /* Set defaults. */
  30.   foregroundColor = NX_COLORBLACK;
  31.   backgroundColor = NX_COLORWHITE;
  32.   customControlView = nil;
  33.  
  34.   return self;
  35. }
  36.  
  37.  
  38. /**------------------------------------*------------------------------------**
  39.     Method:  free
  40.     Info:    Frees all resources allocated by Doodle.
  41.     Return:  (id)self
  42.  **------------------------------------*------------------------------------**/
  43. - free
  44. {
  45.   /*
  46.    * Since customControlView is probably dissociated from the View hierarchy,
  47.    * must free it explicitly. 
  48.    */
  49.   [customControlView free];
  50.  
  51.   return [super free];
  52. }
  53.  
  54.  
  55. /**------------------------------------*------------------------------------**
  56.     Method:  customControlView
  57.     Info:    Provides access to customControlView instance variable. This is a View that contains any special controls that a Doodle might need to adjust its appearance. Doodle expects this outlet to be connected to a View in a nib inside the module package. The nib should have the same name as the class--for instance the Star class uses Star.nib. This nib will be loaded when the customControlView is needed.
  58.     Return:  (id)self
  59.  **------------------------------------*------------------------------------**/
  60. - customControlView
  61. {
  62.   /* If outlet has not been set yet, load nib. */
  63.   if (!customControlView)
  64.     [self loadNib];
  65.  
  66.   return customControlView;
  67. }
  68.  
  69.  
  70. /**------------------------------------*------------------------------------**
  71.     Method:  loadNib
  72.     Info:    Loads Doodle's nib with Doodle as owner. This lets the Doodle connect its customControlView outlet to a View in the nib. Subclasses should provide their own version of a nib in the module package. The nib should have the same name as the class--for instance the Star class uses Star.nib.
  73.     Return:  (id)self, nil if there was a problem
  74.  **------------------------------------*------------------------------------**/
  75. - loadNib
  76. {
  77.   NXBundle           *myBundle;
  78.   char                nibPath[MAXPATHLEN + 1];
  79.  
  80.   /*
  81.    * Get bundle used to load this class. Note that we must use [self class]
  82.    * rather than [Doodle class] to get the actual loaded subclass. 
  83.    */
  84.   myBundle = [NXBundle bundleForClass:[self class]];
  85.  
  86.   /*
  87.    * Get path to Doodle's nib. NXBundle knows how to search .lproj
  88.    * directories inside module packages. 
  89.    */
  90.   [myBundle getPath:nibPath forResource:[[self class] name] ofType:"nib"];
  91.  
  92.   /* Load nib. */
  93.   if ([NXApp loadNibFile:nibPath owner:self withNames:NO fromZone:[self zone]])
  94.   {
  95.     /*
  96.      * Notify self that nib was loaded. This gives subclasses a chance to set
  97.      * instance variables based on settings of controls in nib. 
  98.      */
  99.     [self didLoadNib];
  100.     return self;
  101.   }
  102.   else
  103.     return nil;
  104. }
  105.  
  106.  
  107. /**------------------------------------*------------------------------------**
  108.     Method:  didLoadNib
  109.     Info:    Notifies Doodle that nib has been loaded. This gives subclasses a chance to perform any additional initialization that is dependent on the nib. (This cannot be done with awakeFromNib, because the Doodle is not in the nib, it is the nib's owner!) Typically, Doodle subclasses will use didLoadNib to set instance variables to the default settings of controls in the nib.
  110.     Return:  (id)self
  111.  **------------------------------------*------------------------------------**/
  112. - didLoadNib
  113. {
  114.   return self;
  115. }
  116.  
  117.  
  118. /**------------------------------------*------------------------------------**
  119.     Method:  drawSelf::
  120.     Info:    Basic View drawing method. Just as a developement aid, Doodle fills its bounds with its backgroundColor. Subclasses should override this with their own desired drawing code.
  121.     Return:  (id)self
  122.  **------------------------------------*------------------------------------**/
  123. - drawSelf:(const NXRect *)rects :(int)numRects
  124. {
  125.   NXSetColor(backgroundColor);
  126.   NXRectFill(&bounds);
  127.  
  128.   return self;
  129. }
  130.  
  131.  
  132. /**------------------------------------*------------------------------------**
  133.     Method:  setForegroundColor:, setBackgroundColor:
  134.     Info:    Set values of foregroundColor/backgroundColor instance variables, then update Doodle.
  135.     Return:  (id)self
  136.  **------------------------------------*------------------------------------**/
  137. - setForegroundColor:(NXColor)c
  138. {
  139.   foregroundColor = c;
  140.   [self update];
  141.  
  142.   return self;
  143. }
  144.  
  145.  
  146. - setBackgroundColor:(NXColor)c
  147. {
  148.   backgroundColor = c;
  149.   [self update];
  150.  
  151.   return self;
  152. }
  153.  
  154.  
  155. @end
  156.